package com.ipt.frm.pw;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ipt.frm.pw.bean.Mensaje;
import com.ipt.frm.pw.enumeracion.TipoMensaje;

/**
 * Servlet implementation class LoginServlet
 */
public final class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//Paramettros de inicio del Servlet
	static final String INIT_PARAM_USER_NAME = "username";
	static final String INIT_PARAM_USER_PASS = "userpass";
	//Parametros de la request de la jsp
	static final String LOGIN_JSP_PARAM_USER_NAME = "user";
	static final String LOGIN_JSP_PARAM_USER_PASS = "password";
	
	//Mensajes enviados por request para mostrar al usuario
	static final String MSG = "MSG";
	static Properties propertiesMsg;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
    }
    
    @Override
    public void init() throws ServletException {
    	super.init();
    	

    	propertiesMsg = new Properties();
    	propertiesMsg =(Properties) getServletContext().getAttribute("properties");
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//super.doPost(request, response);
		
		HttpSession session = request.getSession();
		
		Mensaje mensaje;
		
		//recoger valores de la request
		String loginUser = request.getParameter(LOGIN_JSP_PARAM_USER_NAME);
		String loginPass = request.getParameter(LOGIN_JSP_PARAM_USER_PASS);
		
		
		//compararlos 
		
		if(getInitParameter(INIT_PARAM_USER_NAME).equals(loginUser) && 
				getInitParameter(INIT_PARAM_USER_PASS).equals(loginPass)){
			//si validado guardar en ssession + index.jsp
			session.setAttribute(INIT_PARAM_USER_NAME, loginUser);
			session.setAttribute(INIT_PARAM_USER_PASS, loginPass);
			
			String userAgent=request.getHeader("User-Agent");
	    	
			//si es por dispositivo mobil
	    	if (userAgent.indexOf("Mobile")!=-1){
	    		RequestDispatcher dispacher = request.getRequestDispatcher("/movil/index.jsp");
	    		dispacher.forward(request, response);
	    	}
			
	    	else{
			
				//forward index.jsp
				//pasamos el tittle de la pagina
				request.setAttribute("tittle", "Inicio");
				RequestDispatcher dispacher = this.getServletContext().getRequestDispatcher("/index.jsp");
				dispacher.forward(request, response);
	    	}
		}
		else{
			//TODO meter estos literales en un fichero .properties
			//msg error
			mensaje = new Mensaje(TipoMensaje.ERROR,propertiesMsg.getProperty("login.error"));
			
			request.setAttribute(MSG, mensaje);
			request.setAttribute("tittle", "Login");
			//si no validado login.jsp + mseg error
			RequestDispatcher dispacher = this.getServletContext().getRequestDispatcher("/login.jsp");
			dispacher.forward(request, response);
		}
				
	
	}

}
